<template>
  <div class="account-box">
    <Form ref="fm" :validation-schema="rules" v-slot="{ errors }" class="form">
      <!-- 测试 => errors获取表单信息对象 1-没有错误信息是空对象 2-有错误信息 key (双向绑定属性名) :val(错误提示信息) -->
      <!-- <p>{{errors}}</p> -->
      <div class="form-item">
        <div class="input">
          <i class="iconfont icon-user"></i>
          <Field
            v-model="formData.account"
            name="account"
            type="text"
            :class="{ error: errors.account }"
            placeholder="请输入用户名或手机号"
          />
        </div>
        <!-- 表单验证错误信息提示 -->
        <div class="error" v-show="errors.account">
          <i class="iconfont icon-warning" />{{ errors.account }}
        </div>
      </div>
      <div class="form-item">
        <div class="input">
          <i class="iconfont icon-lock"></i>
          <Field
            v-model="formData.password"
            name="password"
            type="password"
            :class="{ error: errors.password }"
            placeholder="请输入密码"
          />
        </div>
        <!-- 表单验证错误信息提示 -->
        <div class="error" v-show="errors.password">
          <i class="iconfont icon-warning" />{{ errors.password }}
        </div>
      </div>
      <div class="form-item">
        <div class="agree">
          <!-- <XtxCheckBox /> -->
          <!-- Field通过as渲染XtxCheckBox组件，同时添加校验 -->
          <Field v-model="formData.isAgree" as="XtxCheckBox" name="isAgree">
            <span>我已同意</span>
          </Field>
          <a href="javascript:;">《隐私条款》</a>
          <span>和</span>
          <a href="javascript:;">《服务条款》</a>
        </div>
        <!-- 表单验证错误信息提示 -->
        <div class="error" v-show="errors.isAgree">
          <i class="iconfont icon-warning" />{{ errors.isAgree }}
        </div>
      </div>
      <a @click="login" href="javascript:;" class="btn">登录</a>
    </Form>
    <div class="action">
      <a href="https://graph.qq.com/oauth2.0/authorize?client_id=100556005&response_type=token&scope=all&redirect_uri=http%3A%2F%2Fwww.corho.com%3A8080%2F%23%2Flogin%2Fcallback">
      <img
        src="https://qzonestyle.gtimg.cn/qzone/vas/opensns/res/img/Connect_logo_7.png"
        alt=""
      />
      </a>
      <!-- <span id="qqLoginBtn"></span> -->
      <div class="url">
        <a href="javascript:;">忘记密码</a>
        <a href="javascript:;">免费注册</a>
      </div>
    </div>
  </div>
</template>

<script>
/**
 * vee-validate 使用步骤
 * 1- 导入两个组件并注册: Form, Field
 * 2- 定义校验规则 => 函数
 * 3- 定义表单和校验规则数据并使用
 */
import { Form, Field } from 'vee-validate'
import { reactive, ref } from 'vue'
import myRules from '@/utils/vee-validate-schema'
import { useStore } from 'vuex'
import { message } from '@/components/Message/index.js'
import { useRoute, useRouter } from 'vue-router'

export default {
  name: 'LoginForm',
  components: {
    Form,
    Field
  },
  setup () {
    const fm = ref(null)
    // 1- 表单数据 (建议使用reactive定义)
    const formData = reactive({
      account: 'xiaotuxian001', // 账号
      password: '123456', // 密码
      isAgree: false // 是否同意用户协议
    })
    // 2- 校验规则 (相当于element-ui 的form组件的rules)
    const rules = {
      account: myRules.account,
      password: myRules.password,
      isAgree (value) {
        if (!value) return '请勾选用户协议'
        // 勾选了
        return true
      }
    }

    // onMounted(() => {
    //   // 组件渲染完毕，使用QC生成QQ登录按钮
    //   window.QC.Login({
    //     btnId: 'qqLoginBtn'
    //   })
    // })

    // 点击登录
    const store = useStore()
    const router = useRouter()
    const route = useRoute()
    const login = async () => {
      // console.log(await fm.value.validate())
      const { valid } = await fm.value.validate()
      if (valid) {
        try {
          await store.dispatch('user/getProfileAction', formData)
          // 登录成功之后 => 合并之前本地购物车数据,到当前登录人名下
          await store.dispatch('cart/mergeCartAction')
          message({ text: '登录成功', type: 'success' })
          router.replace(route.query.redirectUrl || '/')
        } catch (error) {
          console.dir(error)
          message({ type: 'error', text: error.response.data.message })
        }
      }
    }

    return { formData, rules, fm, login }
  }
}
</script>

<style lang="less" scoped>
// 账号容器
.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;
    a {
      color: @xtxColor;
      i {
        font-size: 14px;
      }
    }
  }
  .form {
    padding: 0 20px;
    &-item {
      margin-bottom: 28px;
      .input {
        position: relative;
        height: 36px;
        > i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }
        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;
          &.error {
            border-color: @priceColor;
          }
          &.active,
          &:focus {
            border-color: @xtxColor;
          }
        }
        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }
      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: @priceColor;
        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }
    .agree {
      a {
        color: #069;
      }
    }
    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: @xtxColor;
      &.disabled {
        background: #cfcdcd;
      }
    }
  }
  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}
</style>
